#install packages
library(panelView)
library(zoo)
library(synthdid)
library(fect)
library(ggplot2)
source("staggered_synth.R")
#prepare data
laws = read.csv("statelaws.csv")
turnout = read.csv("stateturnout.csv")
laws = laws[laws$State!="",]
results <- list()

for (i in 1:nrow(laws)) {
  for (j in 2:ncol(laws)) { 
    if (!is.na(laws[i, j])) {
      if (laws[i, j] == "X") {
        results <- c(results, list(data.frame(STATE = laws$State[i], YEAR = colnames(laws)[j], Law = TRUE)))
      }else {
        results <- c(results, list(data.frame(STATE = laws$State[i], YEAR = colnames(laws)[j], Law = FALSE)))
      }
    }else {
      results <- c(results, list(data.frame(STATE = laws$State[i], YEAR = colnames(laws)[j], Law = FALSE)))
    }
  }
}
laws <- do.call(rbind, results)
laws$YEAR <-substring(laws$YEAR,2,10000)

data = merge(laws,turnout,by=c("STATE","YEAR"))
data = data[!(data$STATE %in%c("Texas")),]
data = data[data$YEAR != 1982&data$YEAR != 1980,]
data$Y = as.numeric(gsub(",","",data$VOTE_FOR_HIGHEST_OFFICE))/as.numeric(gsub(",","",data$VEP))*100

names(data)[names(data)=="Law"] <- "W"
names(data)[names(data)=="STATE"] <- "State"
names(data)[names(data)=="YEAR"] <- "Year"



data$W = as.numeric(data$W)

#create early vs late adopting groups
data$group = as.numeric(data$State %in% c("Arizona", "Georgia", "Indiana", "Ohio")) + 2*as.numeric(data$State %in% c("Alabama", "Kansas", "Mississippi", "North Dakota", "Tennessee", "Virginia", "Wisconsin"))


all_fect <- fect(Y ~ W, data = data[data$State != "Virginia" & data$State != "North Dakota",],
                 method = "fe",index = c("State","Year"), CV = FALSE, vartype = "jackknife",
                 se = TRUE, nboots = 2000,  force = "two-way",
                 parallel = TRUE, cores = 8, seed=1, min.T0 = 1, group = "group")
presidential_fect <- fect(Y ~ W, data = data[as.numeric(data$Year)%%4 == 0& data$State != "Virginia" & data$State != "North Dakota",],
                          method = "fe",index = c("State","Year"), CV = FALSE, vartype = "jackknife",
                          se = TRUE, nboots = 2000,  force = "two-way", 
                          parallel = TRUE, cores = 8, seed=1,  group = "group")
midterm_fect <- fect(Y ~ W, data = data[as.numeric(data$Year)%%4 == 2,],
                     method = "fe",index = c("State","Year"), CV = FALSE,
                     se = TRUE, nboots = 2000,  force = "two-way",vartype = "jackknife",
                     parallel = TRUE, cores = 8, seed=1, group = "group")


#synthetic difference-in-differences requires these column names
data$Unit = data$State
data$Time = data$Year

all_sdid = StaggeredSynthDiD(data[data$State != "Virginia" & data$State != "North Dakota",],vcov="jack")
presidential_sdid = StaggeredSynthDiD(data[as.numeric(data$Year)%%4 == 0& data$State != "Virginia" & data$State != "North Dakota",],vcov="jack")
midterm_sdid = StaggeredSynthDiD(data[as.numeric(data$Year)%%4 == 2,],vcov="jack")
all_early_sdid <- StaggeredSynthDiD( data[ data$group != 2,],vcov="jack")
presidential_early_sdid <- StaggeredSynthDiD( data[as.numeric(data$Year)%%4 == 0& data$group != 2,],vcov="jack")
midterm_early_sdid <- StaggeredSynthDiD( data[as.numeric(data$Year)%%4 == 2& data$group != 2,],vcov="jack")
all_late_sdid <- StaggeredSynthDiD( data[data$State != "Virginia" & data$State != "North Dakota" & data$group != 1,],vcov="jack")
presidential_late_sdid <- StaggeredSynthDiD( data[as.numeric(data$Year)%%4 == 0& data$State != "Virginia" & data$State != "North Dakota" & data$group != 1,],vcov="jack")
midterm_late_sdid <- StaggeredSynthDiD( data[as.numeric(data$Year)%%4 == 2& data$group != 1,],vcov="jack")

all_sdid_es = StaggeredSynthDiD_es(data[data$State != "Virginia" & data$State != "North Dakota",],vcov="jack")
presidential_sdid_es = StaggeredSynthDiD_es(data[as.numeric(data$Year)%%4 == 0& data$State != "Virginia" & data$State != "North Dakota",],vcov="jack")
midterm_sdid_es = StaggeredSynthDiD_es(data[as.numeric(data$Year)%%4 == 2,],vcov="jack")
all_early_sdid_es <- StaggeredSynthDiD_es( data[data$group != 2,],vcov="jack")
presidential_early_sdid_es <- StaggeredSynthDiD_es( data[as.numeric(data$Year)%%4 == 0& data$group != 2,],vcov="jack")
midterm_early_sdid_es <- StaggeredSynthDiD_es( data[as.numeric(data$Year)%%4 == 2& data$group != 2,],vcov="jack")
all_late_sdid_es <- StaggeredSynthDiD_es( data[data$State != "Virginia" & data$State != "North Dakota" & data$group != 1,],vcov="jack")
presidential_late_sdid_es <- StaggeredSynthDiD_es( data[as.numeric(data$Year)%%4 == 0& data$State != "Virginia" & data$State != "North Dakota" & data$group != 1,],vcov="jack")
midterm_late_sdid_es <- StaggeredSynthDiD_es( data[as.numeric(data$Year)%%4 == 2& data$group != 1,],vcov="jack")
sink("output.txt", split=TRUE)

cat("\n\n================= ESTIMATION RESULTS =================\n")


all_models <- list(
  "All Years (FECT)" = all_fect,
  "Presidential (FECT)" = presidential_fect,
  "Midterm (FECT)" = midterm_fect,
  "All Years 1 = Early, 2 = Late (FECT)" = all_fect$est.group.att,
  "Presidential 1 = Early, 2 = Late (FECT)" = presidential_fect$est.group.att,
  "Midterm 1 = Early, 2 = Late (FECT)" = midterm_fect$est.group.att,
  "All Years (SDID)" = all_sdid,
  "Presidential (SDID)" = presidential_sdid,
  "Midterm (SDID)" = midterm_sdid,
  "All Early (SDID)" = all_early_sdid,
  "Presidential Early (SDID)" = presidential_early_sdid,
  "Midterm Early (SDID)" = midterm_early_sdid,
  "All Late (SDID)" = all_late_sdid,
  "Presidential Late (SDID)" = presidential_late_sdid,
  "Midterm Late (SDID)" = midterm_late_sdid
)


print_model_summary <- function(name, model) {
  cat("\n------------------------------------------------------\n")
  cat("Model:", name, "\n")
  print(model)
}


for (name in names(all_models)) {
  print_model_summary(name, all_models[[name]])
}
sink()